const NewsModel = require("../../models/NewsModel")

const NewsService = {
  add: async ({ title,content,category,cover,editTime,isPublish }) => {
    return NewsModel.create({
      title,
      content,
      category,
      cover,
      editTime,
      isPublish
    })
  },
  updateList: async ({_id,title,content,category,cover,editTime,isPublish}) => {
    if(cover){
      return NewsModel.updateOne({_id},{title,content,category,cover,editTime,isPublish})
    }else{
      return NewsModel.updateOne({_id},{title,content,category,editTime,isPublish})
    }
  },
  getList: async (_id) => {
    return _id ? NewsModel.findById({_id}) : NewsModel.find({})
  },
  publish:async ({_id,isPublish,editTime}) => {
    return NewsModel.updateOne({_id},{isPublish,editTime})
  },
  delList:async ({_id}) => {
    return NewsModel.deleteOne({_id})
  }
}

module.exports = NewsService